﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Apex.Common
{
    public class LocationServices
    {
        public static LatLon GetCurrentLocation()
        {
            var request = WebRequest.Create(new Uri("http://api.hostip.info/")) as HttpWebRequest;

            using (var webResponse = (request.GetResponse() as HttpWebResponse))
            {
                if (webResponse == null)
                    throw new InvalidOperationException("Invalid web response");

                using (var reader = new StreamReader(webResponse.GetResponseStream()))
                {
                    var doc = new XmlDocument();
                    doc.Load(reader);

                    var nodes = doc.GetElementsByTagName("gml:coordinates");
                    var items = nodes[0].InnerText.Split(',');
                    return new LatLon(items[1], items[0]);
                }
            }
        }
    }
}
